﻿using System;
using System.Collections.Generic;
using Unity.UIWidgets.engine;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using UnityEngine;
using UnityEngine.Networking;
using Transform = Unity.UIWidgets.widgets.Transform;

public class HttpRequestSample : UIWidgetsPanel
{
    protected override Widget createWidget()
    {
        return new MaterialApp(
            title: "Http Request Sample",
            home: new Scaffold(
                appBar: new AppBar(title: new Text("123")
                ),
                body: new AsyncRequestWidget(this.gameObject)
            )
        );
    }
}

public class AsyncRequestWidget : StatefulWidget
{
    public readonly GameObject gameObjOfUIWidgetsPanel;

    public AsyncRequestWidget(GameObject gameObjOfUiWidgetsPanel, Key key = null) : base(key)
    {
        this.gameObjOfUIWidgetsPanel = gameObjOfUiWidgetsPanel;
    }

    public override State createState()
    {
        return new _AsyncRequestWidgetState();
    }
}

[Serializable]
public class TimeData
{
    public long currentFileTime;
}

class _AsyncRequestWidgetState : State<AsyncRequestWidget>
{
    long _fileTime;

    public override Widget build(BuildContext context)
    {
        return new Transform(
            transform: Matrix3.makeScale(2),
            child:new Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: new List<Widget>()
            {
                new FlatButton(child: new Text("Click To Get Time"), onPressed: () =>
                {
                    UnityWebRequest www = UnityWebRequest.Get("http://worldclockapi.com/api/json/est/now");
                    var asyncOperation = www.SendWebRequest();
                    asyncOperation.completed += operation =>
                    {
                        var timeData = JsonUtility.FromJson<TimeData>(www.downloadHandler.text);
                        using (WindowProvider.of(this.widget.gameObjOfUIWidgetsPanel).getScope())
                        {
                            this.setState(() => { this._fileTime = timeData.currentFileTime; });
                        }
                    };
                }),
                new Text($"current file time: {this._fileTime}")
            })
        );
    }
}